//
//  QSJsonParser.m
//  testXML
//
//  Created by apple on 2017/9/28.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "QSJsonParser.h"

@implementation QSJsonParser

/**
 *  把Json文件转换为对象
 */
+ (id)qs_parserJsonFileToObject:(NSString *)fileName fileType:(NSString *)fileType isMainBundleFile:(BOOL)isMainBundleFile
{
    NSString *path = [NSString string];
    
    if (isMainBundleFile) {
        path = [[NSBundle mainBundle] pathForResource:fileName ofType:fileType];
    } else {
        path = fileName;
    }
    
    NSData *date = [NSData dataWithContentsOfFile:path];
    
    NSAssert(date, @"json文件读取失败");
    
    return [self qs_parserJsonDataToObject:date];
}

/**
 *  把Json数据转换为对象
 */
+ (id)qs_parserJsonDataToObject:(NSData *)jsonData
{
    NSError *error = nil;
    id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    
    NSAssert(!error, @"json字符串格式错误");
    
    return jsonObj;
}

/**
 *  把对象转换为Json数据字符串
 */
+ (NSData *)qs_parserObjectToJsonData:(id)object
{
    NSError *parseError = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
